/*
 * Copyright (c) 2014 Luca Agostini, Alessandro Cantini, Alessandro Dignani
 *
 * This file is part of MeteoCal.
 *
 * MeteoCal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.polimi.se2.agostini_cantini_dignani.meteocal.gui;

import it.polimi.se2.agostini_cantini_dignani.meteocal.business.EventManager;
import it.polimi.se2.agostini_cantini_dignani.meteocal.business.UserManager;
import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.Event;
import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.User;
import org.apache.commons.lang3.time.DateUtils;
import org.primefaces.context.RequestContext;

import javax.ejb.EJB;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Alessandro Cantini
 */
abstract public class EventBean extends DateTimeBean {

	protected static final String SAVE_OUTCOME = "/user/home?faces-redirect=true";

	private Event event;
	private String invUser;
	private List<User> invitedUsers;

	@EJB
	private UserManager um;

	@EJB
	private EventManager em;

	public void init() {
		super.init();
		invitedUsers = new ArrayList<>();
	}

	abstract public String saveAction();

	public String checkOverlappingsAndSave() {
		if (isOverlappingEvent()) {
			RequestContext.getCurrentInstance().execute("PF('eventConfirmDialog').show()");
			return null;
		}
		return saveAction();
	}

	public Event getEvent() {
		return event;
	}

	protected void setEvent(Event event) {
		this.event = event;
	}

	public String addInvitedUserAction() {
		if (invUser == null) {
			return null;
		}
		User iu = um.findByEmail(invUser);
		if (iu != null && !invitedUsers.contains(iu) && em.canBeInvited(invUser, event, um.getLoggedUser())) {
			invitedUsers.add(iu);
		}
		invUser = null;
		return null;
	}

	public List<User> getInvitedUsers() {
		List<User> temp = new ArrayList<>();
		if (event != null && event.getInvitedUsers() != null) {
			temp.addAll(event.getInvitedUsers());
		}
		if (invitedUsers != null) {
			temp.addAll(invitedUsers);
		}
		return temp;
	}

	public boolean isOverlappingEvent() {
		return getEm().thereAreOverlappingEvents(getEvent(), getUm().getLoggedUser().getCalendar());
	}

	public void updateEndTime() {
		event.setEndTime(DateUtils.addHours(event.getStartTime(), 1));
	}

	public String getInvUser() {
		return invUser;
	}

	public void setInvUser(String invUser) {
		this.invUser = invUser;
	}

	public boolean isValidInvite() {
		return em.canBeInvited(invUser, event, um.getLoggedUser());
	}

	protected EventManager getEm() {
		return em;
	}

	protected UserManager getUm() {
		return um;
	}

	protected void addInvitedUsersToEvent() {
		for (User u : invitedUsers) {
			em.inviteUserToEvent(event, u);
		}
	}
}
